package com.gome.ss.core.plugin;

import java.util.ArrayList;
import java.util.List;

import com.gome.ss.flowcrawl.plugin.FlowCrawlEnteyPage;
import com.gome.ss.flowcrawl.plugin.FlowCrawlStepPage;
import com.mongodb.DBObject;

public class SpiderLocalPluginStepConfig {
	
	private static final int END_PAGE = 0;
	private static final int LIST_PAGE = 1;
	private static final int ENTRY_PAGE = 2;
	private static final int USUAL_PAGE = 3;
	
	public static final String HREFTYPE = "href-attribute";
	public static final String CLICKTYPE = "click";
	
	private int stepID = 0;
	private int pageType = 0;
	
	private String pluginName = "";
	private String stepName = "";
	private String desc = "";
	private String group = "";
	
	private String nextStepType = "";
	private boolean nextStepOpenNewPage = false;
	private String waitCssListString = "";
	private List<String> waitCssList = new ArrayList<String>();
	private String nextStepCss = "";
	private String nextStepHrefAttribute = "";
	private String startMethod = "";
	private String startUrlString = "";
	private List<String> startUrls = new ArrayList<String>();
	private List<SpiderLocalPluginActionConfig> baction = new ArrayList<SpiderLocalPluginActionConfig>();
	
	private String qualified = "";
	private String qualifiedName = "";
	private String nextStepQualifiedName = "";
	
	private String nextPagesSettingType = ""; // "0" Link String Concatenation; "1" Next Page Css Selector
	
	private String nextPageCss = "";
	private String nextPageType = "";
	private String nextPageHrefAttribute = "";
	
	private String currentPageCss = "";
	private String nextPageParameterPrefix = "";
	private String nextPageParameterSuffix = "";
	private String pageCountStartWith = "";
	private String pageCountCss = "";
	
	private boolean nextPageASync = false;
	private int nextPageClickWaitTime = 0;
	private String nextPageClickWaitCssChanged = "";
	
	private boolean needCarryCookie = false;
	private List<String> carryCookieName = new ArrayList<String>();
	
	private boolean isListPage = false;
	private boolean useIncrementalFilter = false;
	
	private String callbackLink = "";
	private String keyWord = "";
	
	public SpiderLocalPluginStepConfig(String pluginName, String callbackLink, DBObject obj) {
		this.pluginName = pluginName;
		this.callbackLink = callbackLink;
		this.stepID = Integer.valueOf((String) obj.get("stepID"));
		this.pageType = Integer.valueOf((String) obj.get("pageType"));
		
		if (obj.get("isListPage") != null) {
			this.isListPage = (Boolean) obj.get("isListPage");
		}
		
		this.stepName = (String) obj.get("stepName");
		this.desc = (String) obj.get("desc");
		this.group = (String) obj.get("group");
		
		this.nextStepType = (String) obj.get("nextStepType");
		
		if (obj.get("nextStepOpenNewPage") != null)
			this.nextStepOpenNewPage = (Boolean) obj.get("nextStepOpenNewPage");
		
		this.nextStepCss = (String) obj.get("nextStepCss");
		this.nextStepHrefAttribute = (String) obj.get("nextStepHrefAttribute");
		this.startMethod = (String) obj.get("method");
		this.waitCssListString = (String) obj.get("waitCssList");
		if (this.waitCssList != null) {
			String[] waitCssListSet = this.waitCssListString.split(";");
			for (String css : waitCssListSet) {
				this.waitCssList.add(css);
			}
		}
		
		this.startUrlString = (String) obj.get("startUrls");
		
		if (this.startUrlString != null) {
			String[] startUrlSet = this.startUrlString.split("\n");
			for (String url : startUrlSet) {
				this.startUrls.add(url);
			}
		}
		
		List<DBObject> bactionobjs = (List<DBObject>) obj.get("baction");
		for (DBObject bactionobj : bactionobjs) {
			SpiderLocalPluginActionConfig ac = new SpiderLocalPluginActionConfig(bactionobj);
			this.baction.add(ac);
		}
		
		switch (this.pageType) {
		case END_PAGE:
			this.qualified = FlowCrawlStepPage.class.getName();
			break;
		case ENTRY_PAGE:
			this.qualified = FlowCrawlEnteyPage.class.getName();
			break;
		case USUAL_PAGE:
			this.qualified = FlowCrawlStepPage.class.getName();
			break;
		}
		
		if (this.pageType == LIST_PAGE || isListPage) {
			this.qualified = FlowCrawlStepPage.class.getName();
			
			this.nextPagesSettingType = (String) obj.get("nextPagesSettingType");
			this.currentPageCss = (String) obj.get("currentPageCss");
			if (this.nextPagesSettingType.equals("0")) {
				if (obj.get("nextPageParameterPrefix") != null)
					this.nextPageParameterPrefix = (String) obj.get("nextPageParameterPrefix");
				if (obj.get("nextPageParameterSuffix") != null)
					this.nextPageParameterSuffix = (String) obj.get("nextPageParameterSuffix");
				this.pageCountStartWith = (String) obj.get("pageCountStartWith");
				this.pageCountCss = (String) obj.get("pageCountCss");
			} else if (this.nextPagesSettingType.equals("1")) {
				this.nextPageCss = (String) obj.get("nextPageCss");
				this.nextPageType = (String) obj.get("nextPageType");
				this.nextPageHrefAttribute = (String) obj.get("nextPageHrefAttribute");
			}
		}
		
		
		this.needCarryCookie = (Boolean) obj.get("needCarryCookie");
		String carryCookieNameString = (String) obj.get("carryCookieName");
		if (carryCookieNameString != null) {
			String[] carryCookieNameSet = carryCookieNameString.split(";");
			for (String c : carryCookieNameSet) {
				this.carryCookieName.add(c);
			}
		}
		
		if (obj.get("nextPageASync") != null) {
			this.nextPageASync = (Boolean) obj.get("nextPageASync");
			if (obj.get("nextPageClickWaitTime") != null) {
				String nextPageClickWaitTimeString = (String) obj.get("nextPageClickWaitTime");
				this.nextPageClickWaitTime = Integer.valueOf(nextPageClickWaitTimeString);
			}
			if (obj.get("nextPageClickWaitCssChanged") != null) {
				nextPageClickWaitCssChanged = (String) obj.get("nextPageClickWaitCssChanged");
			}
		}
		
		this.qualifiedName = this.pluginName + "." + this.qualified + "." + String.valueOf(this.stepID);
		
		if (obj.get("useIncrementalFilter") != null)
			useIncrementalFilter = (Boolean) obj.get("useIncrementalFilter");
	}
	
	public boolean isEntry() {
		if (pageType == ENTRY_PAGE)
			return true;
		return false;
	}
	
	public boolean isList() {
		if (pageType == LIST_PAGE)
			return true;
		if (isListPage)
			return true;
		return false;
	}
	
	public boolean isEnd() {
		if (pageType == END_PAGE)
			return true;
		return false;
	}
	
	public String getPluginName() {
		return pluginName;
	}

	public void setPluginName(String pluginName) {
		this.pluginName = pluginName;
	}

	public String getCallbackLink() {
		return callbackLink;
	}

	public int getStepID() {
		return stepID;
	}

	public String getStepName() {
		return stepName;
	}

	public String getDesc() {
		return desc;
	}
	
	public String getGroup() {
		return group;
	}

	public int getPageType() {
		return pageType;
	}

	public boolean isNextStepOpenNewPage() {
		return nextStepOpenNewPage;
	}

	public String getNextStepType() {
		return nextStepType;
	}

	public List<String> getWaitCssList() {
		return waitCssList;
	}

	public String getCurrentPageCss() {
		return currentPageCss;
	}

	public boolean isNeedCarryCookie() {
		return needCarryCookie;
	}

	public List<String> getCarryCookieName() {
		return carryCookieName;
	}

	public String getNextStepCss() {
		return nextStepCss;
	}

	public String getNextStepHrefAttribute() {
		return nextStepHrefAttribute;
	}

	public String getStartMethod() {
		return startMethod;
	}

	public String getstartUrlString() {
		return startUrlString;
	}

	public List<String> getStartUrls() {
		return startUrls;
	}

	public List<SpiderLocalPluginActionConfig> getBaction() {
		return baction;
	}

	public String getQualified() {
		return qualified;
	}
	
	public String getQualifiedName() {
		return qualifiedName;
	}
	
	public boolean getProcessable() {
		return this.pageType == ENTRY_PAGE;
	}
	
	public String getStartUrlString() {
		return startUrlString;
	}

	public String getNextPagesSettingType() {
		return nextPagesSettingType;
	}

	public String getNextPageCss() {
		return nextPageCss;
	}
	
	public String getNextPageType() {
		return nextPageType;
	}

	public String getNextPageHrefAttribute() {
		return nextPageHrefAttribute;
	}

	public String getNextPageParameterPrefix() {
		return nextPageParameterPrefix;
	}

	public String getNextPageParameterSuffix() {
		return nextPageParameterSuffix;
	}

	public String getPageCountStartWith() {
		return pageCountStartWith;
	}

	public String getPageCountCss() {
		return pageCountCss;
	}

	public boolean isNextPageASync() {
		return nextPageASync;
	}

	public int getNextPageClickWaitTime() {
		return nextPageClickWaitTime;
	}

	public String getNextPageClickWaitCssChanged() {
		return nextPageClickWaitCssChanged;
	}

	public String getNextStepQualifiedName() {
		return nextStepQualifiedName;
	}

	public void setNextStepQualifiedName(String nextStepQualifiedName) {
		this.nextStepQualifiedName = nextStepQualifiedName;
	}
	
	public boolean isUseIncrementalFilter() {
		return useIncrementalFilter;
	}

	public String getKeyWord() {
		return keyWord;
	}
	
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
}
